package com.jiazhong.mingxing.boot.boot04.test;

import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.core.ListOperations;
import org.springframework.data.redis.core.StringRedisTemplate;

import java.util.List;

@SpringBootTest
@Slf4j
public class ListApp {
    @Resource
    private StringRedisTemplate stringRedisTemplate;

    @Test
    public void a() {
        ListOperations<String, String> forList = stringRedisTemplate.opsForList();
        forList.leftPush("A", "aa");
        forList.leftPush("A", "bb");
        forList.leftPush("A", "cc");
        forList.leftPush("A", "aa");
        forList.leftPush("A", "dd");
        forList.leftPush("A", "ee");
        forList.leftPushAll("A", "ff", "gg", "hh");
        forList.rightPush("A", "11");
        forList.rightPush("A", "22");
        forList.rightPush("A", "33");
        forList.rightPushAll("A", "44", "55", "66");
    }

    @Test
    public void b() {
        ListOperations<String, String> forList = stringRedisTemplate.opsForList();
        // 全取
        List<String> all = forList.range("A", 0, -1);
        log.info("all:{}", all);
        List<String> list1 = forList.range("A", 2, 7);
        log.info("list1:{}", list1);
        String a4 = forList.index("A", 4);
        log.info("a4:{}", a4);
    }

    // 删除
    @Test
    public void c() {
        ListOperations<String, String> forList = stringRedisTemplate.opsForList();
        // 如何删除
        forList.remove("A", 7, "dd");
        // 删除栈顶
        String leftPop = forList.leftPop("A");
        List<String> leftPops = forList.leftPop("A", 3);
        log.info("leftPop:{}", leftPop);
        log.info("leftPops:{}", leftPops);
        // 删除栈尾
        String rightPop = forList.rightPop("A");
        List<String> rightPops = forList.rightPop("A", 2);
        log.info("rightPop:{}", rightPop);
        log.info("rightPops:{}", rightPops);
    }

    @Test
    public void d() {
        ListOperations<String, String> forList = stringRedisTemplate.opsForList();
        Long size = forList.size("A");
        log.info("size:{}", size);
        // 判断是否存在 不存在返回-1
        Long indexOfA = forList.indexOf("A", "aa");
        log.info("indexOfA:{}", indexOfA);
        Long indexOfF = forList.indexOf("A", "ff");
        log.info("indexOfF:{}", indexOfF);
        // 修改制定索引的内容
        forList.set("A", 3, "kk");
        // 截取集合
        forList.trim("A", 1, 3);
        List<String> range = forList.range("A", 0, -1);
        log.info("range:{}", range);


    }
}
